package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.ApUserCollectMapper;
import com.zhentao.mapper.ApUserLikeMapper;
import com.zhentao.model.common.dtos.PageResponseResult;
import com.zhentao.model.common.dtos.ResponseResult;
import com.zhentao.model.common.enums.AppHttpCodeEnum;
import com.zhentao.model.common.vo.FeedbackVo;
import com.zhentao.model.social.dto.SocialDto;
import com.zhentao.model.social.dto.SocialLikeDto;
import com.zhentao.model.social.pojo.ApUserCollect;
import com.zhentao.model.social.pojo.ApUserLike;
import com.zhentao.model.social.pojo.ApUserSocial;
import com.zhentao.model.social.vo.UserLikeVo;
import com.zhentao.service.ApUserSocialService;
import com.zhentao.mapper.ApUserSocialMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 许文倩
* @description 针对表【ap_user_social】的数据库操作Service实现
* @createDate 2024-07-05 09:47:01
*/
@Service
public class ApUserSocialServiceImpl extends ServiceImpl<ApUserSocialMapper, ApUserSocial>
    implements ApUserSocialService{
    @Resource
    ApUserSocialMapper apUserSocialMapper;
    @Resource
    ApUserLikeMapper apUserLikeMapper;
    @Resource
    ApUserCollectMapper apUserCollectMapper;

    @Override
    public ResponseResult getList(SocialDto dto) {
        System.out.println(dto.getPageNum()+"-------------------"+dto.getPageSize()+"");
        Page<ApUserSocial> page=new Page(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<ApUserSocial> wrapper = new QueryWrapper<>();
        wrapper.lambda().like(dto.getContent()!=null,ApUserSocial::getContent,dto.getContent());
        wrapper.lambda().orderByDesc(ApUserSocial::getCreatedTime);
        page = apUserSocialMapper.selectPage(page,wrapper);
        System.err.println(page.getTotal());
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPageNum(), dto.getPageSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        pageResponseResult.setCode(200);
        pageResponseResult.setErrorMessage("查询成功");
        return pageResponseResult;
    }

    @Override
    public ResponseResult addSocial(ApUserSocial apUserSocial) {
        if (apUserSocial==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        apUserSocial.setCreatedTime(new Date());
        apUserSocialMapper.insert(apUserSocial);
        return ResponseResult.okResult("发布成功，待审核");
    }

    @Override
    public ResponseResult loser(SocialDto dto) {
        if (dto==null || dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUserSocial apUserSocial = getById(dto.getId());
        if (apUserSocial==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"动态不存在");
        }
        apUserSocial.setAuditStatus(2);
        apUserSocial.setReason(dto.getMsg());
        apUserSocialMapper.updateById(apUserSocial);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult status(Integer id) {

        ApUserSocial apUserSocial = getById(id);
        if (apUserSocial==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"动态不存在");
        }
        apUserSocial.setAuditStatus(1);
        apUserSocialMapper.updateById(apUserSocial);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult lookStatus(int id) {
        ApUserSocial apUserSocial = apUserSocialMapper.selectById(id);
        if (apUserSocial==null){
            return ResponseResult.errorResult(400,"未找到该动态");
        }
        apUserSocial.setIsDelete(apUserSocial.getIsDelete()==0 ? 1:0);
        apUserSocialMapper.updateById(apUserSocial);
        return ResponseResult.okResult("可见状态成功");
    }

    @Override
    public ResponseResult appList() {
        QueryWrapper<ApUserSocial> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ApUserSocial::getIsDelete,0);
        wrapper.lambda().eq(ApUserSocial::getAuditStatus,1);
        List<ApUserSocial> apUserSocials = apUserSocialMapper.selectList(wrapper);
        return ResponseResult.okResult(apUserSocials);
    }

    @Override
    public ResponseResult updateSocial(ApUserSocial apUserSocial) {
        if (apUserSocial==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        apUserSocialMapper.updateById(apUserSocial);
        return ResponseResult.okResult("动态修改成功，待审核");
    }

    @Override
    public ResponseResult getByUseId() {
        return ResponseResult.okResult(apUserSocialMapper.getByUseId());
    }

    @Override
    public ResponseResult getOneByUseId(Integer userId) {
        List<SocialLikeDto> userLikeVo=apUserSocialMapper.getOneByUseId(userId);
        for (SocialLikeDto likeDto:userLikeVo){
            QueryWrapper<ApUserLike> wrapper=new QueryWrapper<>();
            wrapper.lambda().eq(ApUserLike::getSocialId,likeDto.getId());
            wrapper.lambda().eq(ApUserLike::getLikeUserId,userId);
            ApUserLike apUserLike = apUserLikeMapper.selectOne(wrapper);
            if (apUserLike!=null){
                likeDto.setLikeStatus(1);
            }else {
                likeDto.setLikeStatus(0);
            }
        }
        return ResponseResult.okResult(userLikeVo);
    }

    @Override
    public ResponseResult notChecklist(SocialDto dto) {
        System.out.println(dto.getPageNum()+"-------------------"+dto.getPageSize()+"");
        Page<ApUserSocial> page=new Page(dto.getPageNum(),dto.getPageSize());
        QueryWrapper<ApUserSocial> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ApUserSocial::getIsDelete,0);
        wrapper.lambda().eq(ApUserSocial::getAuditStatus,0);
        page = apUserSocialMapper.selectPage(page,wrapper);
        System.err.println(page.getTotal());
        PageResponseResult pageResponseResult = new PageResponseResult(dto.getPageNum(), dto.getPageSize(), (int) page.getTotal());
        pageResponseResult.setData(page.getRecords());
        pageResponseResult.setCode(200);
        pageResponseResult.setErrorMessage("查询成功");
        return pageResponseResult;
    }

    @Override
    public ResponseResult applist(SocialDto dto) {
        Integer pageSize = dto.getPageSize();
        if (ObjectUtils.isEmpty(pageSize) || pageSize==0){
            pageSize=10;
        }
        dto.setPageSize(pageSize);
        List<SocialLikeDto> list=apUserSocialMapper.selectListPage(dto);
        for (SocialLikeDto likeDto:list){
            QueryWrapper<ApUserLike> wrapper=new QueryWrapper<>();
            wrapper.lambda().eq(ApUserLike::getSocialId,likeDto.getId());
            wrapper.lambda().eq(ApUserLike::getLikeUserId,dto.getUserId());
            ApUserLike apUserLike = apUserLikeMapper.selectOne(wrapper);
            if (apUserLike!=null){
                likeDto.setLikeStatus(1);
            }else {
                likeDto.setLikeStatus(0);
            }
            QueryWrapper<ApUserCollect> queryWrapper=new QueryWrapper<>();
            queryWrapper.lambda().eq(ApUserCollect::getUserId,dto.getUserId());
            queryWrapper.lambda().eq(ApUserCollect::getLikeId,likeDto.getUserId());
            ApUserCollect apUserCollect = apUserCollectMapper.selectOne(queryWrapper);
            if (apUserCollect!=null){
                likeDto.setCollectStatus(1);
            }else {
                likeDto.setCollectStatus(0);
            }
        }

        return  ResponseResult.okResult(list);
    }

    @Override
    public ResponseResult getFeedback(Integer userId) {
        List<FeedbackVo> feedback= apUserSocialMapper.getFeedback(userId);
        return ResponseResult.okResult(feedback);
    }


    public ApUserSocial getById(Integer id){
        ApUserSocial apUserSocial = apUserSocialMapper.selectById(id);
        return apUserSocial;
    }


}




